package br.com.passeionaweb.android.TabelaFipe;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SeachActivity extends Activity {

	private static final int DIALOG_LOADING = 3;
	private static final int DIALOG_MAKES = 4;
	private static final int DIALOG_MODELS = 5;
	private static final int DIALOG_YEARS = 6;
	private static final int DIALOG_VERSIONS = 7;

	private DataProvider data;
	private String[] makes = new String[] {};
	private String[] models = new String[] {};
	private String[] years = new String[] {};
	private String[] versions = new String[] {};
	private String price;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		data = new DataProvider(this);
		setupScreen();
	}

	@Override
	protected void onResume() {
		
		super.onResume();
	}

	private void setupScreen() {
		setContentView(R.layout.main);
		((Button) findViewById(R.id.btnMake)).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (makes != null) {
					showDialog(DIALOG_MAKES);
				} else {
					updateMakes();
				}
			}

		});
		((Button) findViewById(R.id.btnModel)).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (models != null) {
					showDialog(DIALOG_MODELS);
				} else {
					updateModels();
				}
			}

		});
		((Button) findViewById(R.id.btnYear)).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (years != null) {
					showDialog(DIALOG_YEARS);
				} else {
					updateYears();
				}

			}

		});
		((Button) findViewById(R.id.btnVersion)).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (versions != null) {
					showDialog(DIALOG_VERSIONS);
				} else {
					updateVersions();
				}
			}

		});
		updateMakes();
	}

	private void updateMakes() {
		clearMake();
		showDialog(DIALOG_LOADING);
		new GetMakesTask().execute(new String[1]);
	}

	private void updateModels() {
		clearModel();
		showDialog(DIALOG_LOADING);
		new GetModelsTask().execute(new String[1]);
	}

	private void updateYears() {
		clearYear();
		showDialog(DIALOG_LOADING);
		new GetYearsTask().execute(new String[1]);
	}

	private void updateVersions() {
		clearVersion();
		showDialog(DIALOG_LOADING);
		new GetVersionsTask().execute(new String[1]);
	}

	private void updatePrice() {
		clearPrice();
		showDialog(DIALOG_LOADING);
		new GetPriceTask().execute(new String[1]);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog;
		switch (id) {
			case DIALOG_LOADING:
				dialog = new ProgressDialog(this);
				((ProgressDialog) dialog).setMessage(getString(R.string.dialog_loading));
				return dialog;
			case DIALOG_MAKES:
				dialog = new AlertDialog.Builder(this).setItems(makes, new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						setMake(which);
						removeDialog(DIALOG_MAKES);
					}
				}).create();
				return dialog;
			case DIALOG_MODELS:
				dialog = new AlertDialog.Builder(this).setItems(models, new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						setModel(which);
						removeDialog(DIALOG_MODELS);
					}
				}).create();
				return dialog;
			case DIALOG_YEARS:
				dialog = new AlertDialog.Builder(this).setItems(years, new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						setYear(which);
						removeDialog(DIALOG_YEARS);
					}
				}).create();
				return dialog;
			case DIALOG_VERSIONS:
				dialog = new AlertDialog.Builder(this).setItems(versions, new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						setVersion(which);
						removeDialog(DIALOG_VERSIONS);
					}
				}).create();
				return dialog;
		}
		return super.onCreateDialog(id);
	}

	public void setMake(String make) {
		data.setMake(make);
		((TextView) findViewById(R.id.txtMake)).setText(make);
		if (!make.equals("")) {
			updateModels();
		}
	}

	public void setMake(int makeIndex) {
		setMake(makes[makeIndex]);
	}

	public void setModel(String model) {
		data.setModel(model);
		((TextView) findViewById(R.id.txtModel)).setText(model);
		if (!model.equals("")) {
			updateYears();
		}
	}

	public void setModel(int modelIndex) {
		setModel(models[modelIndex]);
	}

	public void setYear(String year) {
		data.setYear(year);
		((TextView) findViewById(R.id.txtYear)).setText(year);
		if (!year.equals("")) {
			updateVersions();
		}
	}

	public void setYear(int yearIndex) {
		setYear(years[yearIndex]);
	}

	public void setVersion(int yearIndex) {
		setVersion(versions[yearIndex]);
	}

	public void setVersion(String version) {
		data.setVersion(version);
		((TextView) findViewById(R.id.txtVersion)).setText(version);
		if (!version.equals("")) {
			updatePrice();
		}
	}

	public void setPrice(String price) {
		this.price = price;
		((TextView) findViewById(R.id.txtPrice)).setText(this.price);
	}

	private void clearMake() {
		makes = null;
		setMake("");
		clearModel();
		((Button) findViewById(R.id.btnModel)).setEnabled(false);
	}

	private void clearModel() {
		models = null;
		setModel("");
		clearYear();
		((Button) findViewById(R.id.btnYear)).setEnabled(false);
	}

	private void clearYear() {
		years = null;
		setYear("");
		clearVersion();
		((Button) findViewById(R.id.btnVersion)).setEnabled(false);
	}

	private void clearVersion() {
		versions = null;
		setVersion("");
		clearPrice();
	}

	private void clearPrice() {
		setPrice("");
	}

	private class GetMakesTask extends GetDataTask {

		@Override
		protected void doAction() throws Exception {
			makes = data.getMakes();
		}

		@Override
		protected void onError() {
			Toast.makeText(getApplicationContext(), getString(R.string.message_error_makes),
					Toast.LENGTH_LONG).show();

		}

		@Override
		protected void onSuccess() {
			((Button) findViewById(R.id.btnMake)).setEnabled(true);
			showDialog(DIALOG_MAKES);

		}

	}

	private class GetModelsTask extends GetDataTask {

		@Override
		protected void doAction() throws Exception {
			models = data.getModels();
		}

		@Override
		protected void onError() {
			Toast.makeText(getApplicationContext(), getString(R.string.message_error_models),
					Toast.LENGTH_LONG).show();
		}

		@Override
		protected void onSuccess() {
			((Button) findViewById(R.id.btnModel)).setEnabled(true);
			showDialog(DIALOG_MODELS);
		}

	}

	private class GetYearsTask extends GetDataTask {

		@Override
		protected void doAction() throws Exception {
			years = data.getYears();
		}

		@Override
		protected void onError() {
			Toast.makeText(getApplicationContext(), getString(R.string.message_error_years),
					Toast.LENGTH_LONG).show();
		}

		@Override
		protected void onSuccess() {
			((Button) findViewById(R.id.btnYear)).setEnabled(true);
			showDialog(DIALOG_YEARS);
		}
	}

	private class GetVersionsTask extends GetDataTask {

		@Override
		protected void doAction() throws Exception {
			versions = data.getVersions();
		}

		@Override
		protected void onError() {
			Toast.makeText(getApplicationContext(), getString(R.string.message_error_versions),
					Toast.LENGTH_LONG).show();
		}

		@Override
		protected void onSuccess() {
			((Button) findViewById(R.id.btnVersion)).setEnabled(true);
			showDialog(DIALOG_VERSIONS);
		}
	}

	private class GetPriceTask extends GetDataTask {

		private String price;

		@Override
		protected void doAction() throws Exception {
			this.price = data.getPrice();

		}

		@Override
		protected void onError() {
			Toast.makeText(getApplicationContext(), getString(R.string.message_error_price),
					Toast.LENGTH_LONG).show();

		}

		@Override
		protected void onSuccess() {
			setPrice(this.price);

		}
	}

	private abstract class GetDataTask extends AsyncTask<String, Integer, Integer> {
		private static final int RESULT_OK = 0;
		private static final int RESULT_ERROR = 1;

		@Override
		protected final Integer doInBackground(String... params) {
			try {
				doAction();
				return RESULT_OK;
			} catch (Exception e) {
				return RESULT_ERROR;
			}
		}

		@Override
		protected final void onPostExecute(Integer result) {
			dismissDialog(DIALOG_LOADING);
			switch (result) {
				case RESULT_OK:
					onSuccess();
					break;
				case RESULT_ERROR:
					onError();
					break;
			}
			super.onPostExecute(result);
		}

		protected abstract void doAction() throws Exception;

		protected abstract void onSuccess();

		protected abstract void onError();

	}

}